package com.li.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.li.domain.SystemAddress;
import com.li.service.SystemAddressService;
import com.li.utlis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("systemAddress")
public class SystemAddressController {

    @Autowired
    private SystemAddressService systemAddressService;

    /**
     * 分页模糊查询数据
     * @param systemAddress
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/listPageSystemAddress")
    public JSONObject listPageSystemAddress(SystemAddress systemAddress, Integer page, Integer limit){
        List<SystemAddress> list = systemAddressService.listPageSystemAddress(systemAddress,page,limit);
        Integer count = systemAddressService.listPageSystemAddressCount(systemAddress);
        return R.listPage(list,count);
    }

    /**
     * 添加或修改
     * @param systemAddress
     * @return
     */
    @RequestMapping("/saveOrUpdateSystemAddress")
    public JSONObject saveOrUpdateSystemAddress(SystemAddress systemAddress){
        try {
            if (systemAddress.getId()==null){
                systemAddressService.addSystemAddress(systemAddress);
            }else {
                systemAddressService.updateSystemAddress(systemAddress);
            }
        }catch (Exception e){
            return R.getError(systemAddress);
        }
        return R.getSuccess();
    }

    /**
     * 根据id查询一条
     * @param id
     * @return
     */
    @GetMapping("/editSystemAddress/{id}")
    public SystemAddress editSystemAddress(@PathVariable("id") Integer id){
        return systemAddressService.getById(id);
    }

    /**
     * 根据id删除一条
     * @param id
     * @return
     */
    @GetMapping("/delSystemAddress/{id}")
    public JSONObject delSystemAddress(@PathVariable("id") Integer id){
        boolean flag=systemAddressService.removeById(id);
        if (flag){
            return R.getSuccess();
        }else{
            return R.getError(id);
        }
    }

}












